var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var newsid;
var listOffset;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsLoaded: false,
    selfcomment: '',
    comments: [],
    comment_tid: 0,
    comment_tname: '',
    canCollect: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!options.newsid){
      return;
    }
    newsid = options.newsid;
    listOffset = 0;
    this.setData({
      userid: app.globalData.uid
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      newsLoaded: false
    });
    app.showLoading();
    app.ready(()=>{
      var url = '/api/Article/detail';
      var params = {
        article_id: newsid
      };
      app.request(url, params).then(this.loadNewsDetailCallBack.bind(this));

      this.loadCommentList(listOffset)
    });
  },
  loadNewsDetailCallBack:function(res){
    app.hideLoading();
    if(res.status != 'ok' || !res.result){
      wx.navigateBack({});
      return;
    }
    var result = res.result;
    result.publish_time = app.formatDateTime(result.publish_time,false);
    this.setData({
      newsinfo:result,
      newsLoaded: true
    });
    var article = app.repicSrc(result.content);
    article = article.replace(/&nbsp;/g, '');
    article = article.replace(/amp;/g, '');
    WxParse.wxParse('article', 'html', article, this, 0);

    var that = this;
    var delayId = setTimeout(function(){
      clearTimeout(delayId);
      that.setData({
        canCollect: true
      });
    },500);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  addComment: function () {
    var nowT = (new Date()).getTime();
    if(app.globalData.lastCommentStamp){
      var lastT = app.globalData.lastCommentStamp;
      if(nowT - lastT < 3){
        app.toast.error('评论过于频繁');
        return;
      }
    }
    app.globalData.lastCommentStamp = nowT;
    var params = {
      article_id: newsid,
      message: this.data.selfcomment
    };
    var tops = '回复 ' + this.data.comment_tname + ':';
    if (this.data.selfcomment.indexOf(tops) >= 0 && this.data.comment_tid > 0) {
      this.data.selfcomment = this.data.selfcomment.replace(tops, "");
      params.username = app.globalData.nickName;
      params.message = this.data.selfcomment.trim();
    }
    if (params.message == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入评论',
      });
      return;
    }
    app.showLoading();
    var url = '/api/Article/addcomment';
    app.request(url,params).then(this.addCommentCallBack.bind(this));
  },
  addTcomment: function (event) {
    var tid = event.currentTarget.dataset.pid;
    var tname = event.currentTarget.dataset.tname;
    var selfcomment = '回复 ' + tname + ':';
    this.setData({
      comment_tid: tid,
      comment_tname: tname,
      selfcomment: selfcomment
    });
  },
  addCommentCallBack: function (res) {
    app.hideLoading();
    if (res.status == 'ok') {
      app.toast.success('评论成功');
      this.setData({
        selfcomment: ''
      });
      listOffset = 0;
      this.morebtn_tap();
    }
  },
  delcomment: function (event) {
    var params = {
      article_id: event.currentTarget.dataset.pid
    };
    var url = '/api/Article/delcomment';
    app.request(url,params).then(this.delCommentCallBack.bind(this));
  },
  delCommentCallBack: function (res) {
    wx.showToast({
      icon: 'none',
      title: res.msg,
    });
    if (res.status == 'ok') {
      listOffset = 0;
      this.morebtn_tap();
    }
  },
  commentInput: function (event) {
    var cmm = event.detail.value;
    this.setData({
      selfcomment: cmm
    });
  },
  loadCommentList: function (offset) {
    var url = '/api/Article/article_comm_list';
    var params = {
      article_id: newsid,
      page: offset
    }
    app.request(url, params).then(this.commentlistCallBack.bind(this));
  },
  commentlistCallBack: function (res) {
    app.hideLoading();
    var dataArr = res.result.arr;
    var datararr = res.result.rarr;
    var comments = [];
    if (listOffset > 0) {
      comments = this.data.comments;
    }
    for (var i = 0; i < dataArr.length; i++) {
      var cmminfo = {};
      cmminfo.id = dataArr[i].id;
      cmminfo.name = dataArr[i].nickname;
      cmminfo.userid = dataArr[i].uid;
      cmminfo.avatar = dataArr[i].head_pic;
      if (cmminfo.avatar == '') {
        cmminfo.avatar = '../../imgs/demoheadicon.png';
      }
      cmminfo.text = dataArr[i].message;
      cmminfo.time = app.formatDateTime(dataArr[i].add_time);
      comments.push(cmminfo);
    }
    var rcomments = {};
    var rcommentsArr = [];
    var rid = 0;
    for (var i = 0; i < datararr.length; i++) {
      var cmminfo = {};
      rid = datararr[i].parent_id;
      if (rcomments[rid] == null || rcomments[rid] == undefined) {
        rcomments[rid] = [];//rcommentscmminfo;        
      }
      cmminfo.id = datararr[i].id;
      cmminfo.name = datararr[i].username;
      cmminfo.tname = datararr[i].tusername;
      cmminfo.userid = datararr[i].uid;
      cmminfo.text = datararr[i].message;
      cmminfo.time = app.formatDateTime(datararr[i].add_time);
      rcomments[rid].push(cmminfo);
      console.log('impush=' + rid + 'iid=' + datararr[i].parent_id);
    }
    this.setData({
      comments: comments,
      rcomments: rcomments
    });
  },
  morebtn_tap: function () {
    if (this.data.comments.length < 10) {
      listOffset = 0;
    }
    else {
      listOffset++;
    }
    app.showLoading();
    this.loadCommentList(listOffset);
  },
  addCollect:function(e){
    var url = '/api/Article/collect_article';
    var params = {
      article_id: newsid
    };
    app.request(url, params).then(this.addCollectCallBack.bind(this));
  },
  addCollectCallBack:function(res){
    console.log(res);
    if(res.status == 'ok'){
      app.toast.success('收藏成功');
    }
    else{
      app.toast.error(res.msg);
    }
  }
})